import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/Login',
    component: () => import('../views/Login.vue'),
    hidden: true
  },
  {
    path: '/Home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: () => import('../views/welcome.vue') },
      { path: '/users', component: () => import('../views/users.vue') },
      { path: '/rights', component: () => import('../views/power/rights.vue') },
      { path: '/roles', component: () => import('../views/power/roles.vue') },
      { path: '/orders', component: () => import('../views/order/orders.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/Login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/Login')
  next()
})

export default router
